package com.chuck.blogpro.controller;


import com.chuck.blogpro.pojo.Result;
import com.chuck.blogpro.pojo.User;
import com.chuck.blogpro.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户管理
 */
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    // 获取所有用户信息
    @GetMapping
    public Result users() {
        log.info("获取用户信息");
        List<User> users = userService.getUsers();
        log.info("获取用户信息成功");
        return Result.success(users);
    }

    // 根据 id 获取用户信息
    @GetMapping("/{id}")
    public Result user(@PathVariable Integer id) {
        log.info("根据 id 获取用户信息");
        User user = userService.getUserById(id);
        log.info("根据 id 获取用户信息成功");
        return Result.success(user);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除用户信息");
        userService.delete(id);
        log.info("删除用户信息成功");
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("更新用户信息");
        userService.update(user);
        log.info("更新用户信息成功");
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody User user) {
        log.info("添加用户信息 {}", user);
        userService.add(user);
        log.info("添加用户信息成功");
        return Result.success();
    }
}
